from requests import get
import json
import os
import time
import codecs

def get_total():
    url='https://www.zhihu.com/api/v4/questions/295553454/answers'
    data={
            'include':'data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics',
            'limit':5,
            'offset':5,
            'sort_by':'default'
        }
    # 传递头信息
    headers = {
         # 可以使服务器识别客户使用的操作系统及版本、浏览器及版本等信息。在作爬虫时加上此信息，可以伪装成浏览器
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
    r = get(url,data,headers=headers)
    data_dict = json.loads(r.text)
    totals = data_dict["paging"]["totals"]
    print(totals)
    return totals

def get_one_page(url,data):
    # 传递头信息
     headers = {
         # 可以使服务器识别客户使用的操作系统及版本、浏览器及版本等信息。在作爬虫时加上此信息，可以伪装成浏览器
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
     }
     r = get(url,data,headers=headers)

     if r.status_code == 200:
         data_dict = json.loads(r.text)
      
         totals = data_dict["paging"]["totals"]
         print(totals)
         pages = round(totals/5)
         print(pages)
         filePath = createFolder()
         for i in data_dict["data"]:
             # 创建 txt 文件
           
            print(i["id"])
            filename = filePath+"\\"+str(i["id"])+'.txt'
            target = codecs.open(filename,'w','utf-8')
            strcontent = i['content']
            target.write(strcontent)
     return None
     
def createFolder():
    filePath="E:\\zhihu_file_295553454\\"
    state=os.path.exists(filePath)               #判断路径是否存在
    if state:
        print("File Exist!")
    else:
        os.makedirs(filePath)                    #创建目录
    return filePath
 
  
   

def main():
    url='https://www.zhihu.com/api/v4/questions/295553454/answers'
    offset = 0
    totals = get_total()
    n = round(totals/5)
    for o in range(0,n):
        #offset 代表偏移量值
       offset = o*5
       data={
            'include':'data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics',
            'limit':5,
            'offset':offset,
            'sort_by':'default'
        }

        # https://www.zhihu.com/question/295553454
       get_one_page(url,data)
       time.sleep(3)



